import java.util.Scanner;

/**
 * @Author 12629
 * @Description：
 */
public class Test4 {

    public static void main(String[] args) {
       /* try {
            func();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }*/
        func();
        System.out.println("after try catch");
    }

    public static void func() {
        int[] arr = {1, 2, 3};
        System.out.println(arr[20]);
    }





    public static int func2(int a) {
        try {
            if(a == 0) {
               throw new ArithmeticException();
            }
            return a;
        }catch (ArithmeticException e) {
            System.out.println("算术异常！");
        }finally {
            //避免以后在这里进行return 这不是一个好的习惯
            return 19;
        }
    }

    public static void main3(String[] args) {
        System.out.println(func2(10));
    }
    public static void main2(String[] args) {
        //此时会自动帮你 关掉资源的
        try (Scanner scanner = new Scanner(System.in)) {
            int[] array = {1, 2, 3};
            System.out.println(array.length);
            System.out.println("hello!");
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("捕获了一个算术异常！");
        } catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕获到了一个空指针异常");
        } finally {
            System.out.println("不管程序是否抛出异常，finally都会被执行，进行资源的关闭！");
        }
        System.out.println("我是其他的代码逻辑！");
    }

    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int[] array = {1,2,3};
            System.out.println(array.length);
            System.out.println("hello!");
        }catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("捕获了一个算术异常！");
        }catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕获到了一个空指针异常");
        }finally {
            scanner.close();
            System.out.println("不管程序是否抛出异常，finally都会被执行，进行资源的关闭！");
        }
        System.out.println("我是其他的代码逻辑！");
    }
}
